package com.lianyu.wangyiyun.utils;

import android.Manifest;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Build;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowInsetsController;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager2.adapter.FragmentStateAdapter;

import java.util.List;

/**
 * @author Chen Yuguang
 * @date 2021/5/6 5:24
 */
public class MyUtils {

    private static final int REQUEST_EXTERNAL_STORAGE = 1;

    private static final String[] PERMISSIONS_STORAGE = {
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.RECORD_AUDIO
    };

    /**
     * 获取写的权限
     *
     * @param activity 获取权限的活动
     * @author Chen Yuguang
     * @date 2021/5/7 3:16
     */
    public static void verifyStoragePermissions(Activity activity) {
        try {
            //检测是否有写的权限
            int permission = ActivityCompat.checkSelfPermission(activity,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE);
            if (permission != PackageManager.PERMISSION_GRANTED) {
                // 没有写的权限，去申请写的权限，会弹出对话框
                ActivityCompat.requestPermissions(activity, PERMISSIONS_STORAGE, REQUEST_EXTERNAL_STORAGE);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 为按钮设置按钮动画
     *
     * @param view 按钮动画的按钮对象
     * @author Chen Yuguang
     * @date 2021/5/7 3:18
     */
    public static void btnAnimator(View view) {
        ObjectAnimator.ofFloat(view, "scaleX", 1, 0.8f, 1.3f, 0.9f, 1).setDuration(500).start();
        ObjectAnimator.ofFloat(view, "scaleY", 1, 0.8f, 1.3f, 0.9f, 1).setDuration(500).start();
    }

    /**
     * 沉浸状态栏
     *
     * @param activity 要沉浸状态栏的活动
     * @param isBlack  状态栏是否黑体
     * @author Chen Yuguang
     * @date 2021/4/1 6:51
     */
    public static void immerseStatusBar(@NonNull Activity activity, boolean isBlack) {
        Window window = activity.getWindow();
        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            window.setDecorFitsSystemWindows(false);
            WindowInsetsController ic = window.getInsetsController();
            if (ic != null) {
                if (isBlack) {
                    // 设置状态栏黑色字体
                    ic.setSystemBarsAppearance(WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS, WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS);
                } else {
                    // 默认状态栏白色字体
                    ic.setSystemBarsAppearance(0, WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS);
                }
            }
        } else {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
                // 设置状态栏黑色字体
                if (isBlack) {
                    option = option | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
                }
                window.getDecorView().setSystemUiVisibility(option);
            }
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            // 设置状态栏背景透明
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            activity.getWindow().setStatusBarColor(Color.TRANSPARENT);
        }
    }

    /**
     * 获取状态栏高度
     *
     * @param activity 要获取状态栏高度的活动
     * @return int
     * @author Chen Yuguang
     * @date 2021/4/1 6:52
     */
    public static int getStatusBarHeight(@NonNull Activity activity) {
        int resourceId = activity.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            return activity.getResources().getDimensionPixelSize(resourceId);
        }
        return 0;
    }

    /**
     * 自定义页面适配器
     *
     * @author Chen Yuguang
     * @date 2021/4/1 6:52
     */
    public static class MyFragmentStateAdapter extends FragmentStateAdapter {
        private final List<Fragment> mFragments;

        public MyFragmentStateAdapter(@NonNull FragmentActivity fragmentActivity, List<Fragment> fragments) {
            super(fragmentActivity);
            this.mFragments = fragments;
        }

        @NonNull
        @Override
        public Fragment createFragment(int position) {
            return mFragments.get(position);
        }

        @Override
        public int getItemCount() {
            return mFragments.size();
        }
    }

    /**
     * 动态设置Margin
     *
     * @param v 要设置Margin的View
     * @param l 左Margin
     * @param t 上Margin
     * @param r 右Margin
     * @param b 下Margin
     * @author Chen Yuguang
     * @date 2021/4/1 6:53
     */

    public static void setMargins(@NonNull View v, int l, int t, int r, int b) {
        if (v.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
            ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
            p.setMargins(l, t, r, b);
            v.requestLayout();
        }
    }

    /**
     * 动态设置宽高（为0默认）
     *
     * @param v 要设置宽高的View
     * @param h 高度
     * @param w 宽度
     * @author Chen Yuguang
     * @date 2021/4/1 6:54
     */
    public static void setSize(@NonNull View v, int h, int w) {
        ViewGroup.LayoutParams layoutParams = v.getLayoutParams();
        if (h != 0) {
            layoutParams.height = h;
        }
        if (w != 0) {
            layoutParams.width = w;
        }
        v.setLayoutParams(layoutParams);
        v.requestLayout();
    }

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     *
     * @param context 用来获取分辨率的内容
     * @param dpValue dp值
     * @return int
     * @author Chen Yuguang
     * @date 2021/4/7 2:07
     */
    public static int dp2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
     *
     * @param context 用来获取分辨率的内容
     * @param pxValue px值
     * @return int
     * @author Chen Yuguang
     * @date 2021/4/7 2:07
     */
    public static int px2dp(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }
}
